package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.system.entity.BtyTxl;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyAqjjMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.GcgWsjdMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ProjectMapper;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class BtyAqjjService {

    @Autowired
    private BtyAqjjMapper mapper;
    @Autowired
    private GcgWsjdMapper gcgWsjdMapper;
    @Autowired
    private ProjectMapper projectMapper;
    /**
     * 获取档案管理列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:05 PM
     */

    public Page<Map<String, Object>> list(String ctbh, String dm) {
        Page page = LayuiPageFactory.defaultPage();
        return this.mapper.list(page, ctbh, dm);
    }

    public List<BtyTxl> listExport(String ctbh, String dm){
        return this.mapper.listExport( ctbh, dm);
    }


    public BtyTxl getByID(Long id){
        return this.mapper.getByID(id);
    }

    public int update(BtyTxl btyTxl){
        return this.mapper.update(btyTxl);
    }

    /**
     * 同步法人及卫生许可证信息
     * @return
     */
    public ResponseData updateTxl() {
        //获取法人姓名
        List<Map> frList = gcgWsjdMapper.getFrList();
        //获取卫生许可证时间
        List<Map> wsList = projectMapper.getWsList();

        //修改法人信息
        if(ToolUtil.isNotEmpty(frList)){
            //每150条保存一次
            for(int i = 0; i < frList.size();){
                int i2 = (i + 150);
                i2 = i2 > frList.size() ? frList.size() : i2;
                List<Map> sub = frList.subList(i, i2);
                //修改通讯录中信息
                this.mapper.updateFr(sub);
                i = i2;
            }
        }

        //修改卫生许可证信息
        if(ToolUtil.isNotEmpty(wsList)){
            //每150条保存一次
            for(int i = 0; i < wsList.size();){
                int i2 = (i + 150);
                i2 = i2 > wsList.size() ? wsList.size() : i2;
                List<Map> sub = wsList.subList(i, i2);
                //修改通讯录中信息
                this.mapper.updateWs(sub);
                i = i2;
            }
        }

        return ResponseData.success();
    }
}
